package com.algorithm;

/**
 * 乘积最大子数组
 */
public class MaximumProductSubarray {

    public static int maxProduct(int[] nums) {
        int max = nums[0];
        int [] maxTemp =new int[nums.length];
        int [] minTemp =new int[nums.length];
        maxTemp[0] = nums[0];
        //最小值（负数的情况）
        minTemp[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            maxTemp[i]=Math.max(Math.max(nums[i],nums[i]*maxTemp[i-1]),nums[i]*minTemp[i-1]);
            minTemp[i]=Math.min(Math.min(nums[i],nums[i]*maxTemp[i-1]),nums[i]*minTemp[i-1]);
            max=Math.max(max,maxTemp[i]);
        }
        return max;
    }

    public static void main(String[] args) {
//        int[] nums = {2, 3, -2, 4};
        int[] nums = {-4,-3,-2};
        int result = maxProduct(nums);
        System.out.println(result);
    }
}
